package com.fingard.dsp.bank.directbank.weixin01;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.text.XmlHelper;
import com.fingard.xml.XmlTextReader;

import java.security.PublicKey;
import java.util.ArrayList;

public class WEIXIN01SinglePay extends WEIXIN01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "wxpay");
            if (actItem == null) {
                retDetail.respCode = "0";
                retDetail.respInfo = "未找到账号:[" + p_req1908.singleDetail.payAct + "]商户配置信息,请检查'config/portfiles/WEIXIN01/config.xml'中'actSet'节点相关配置!";
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = FailCode.E9981;
                retAts.writeRetDetail(retDetail);
                return retAts;
            }

            ArrayList<String> arrayParam = new ArrayList<String>();
            StringBuilder sbXml = new StringBuilder();
            String serviceUrlTrgFro = "";
            String amount = StringHelper.amountMultiply100(p_req1908.singleDetail.payAmount);
            if ("190802".equals(p_req1908.reqHeader.commandCode) || "WXTransfer01".equals(p_req1908.reqHeader.commandCode)) {
                // 企业付款到零钱
                serviceUrlTrgFro = "promotion";
                sbXml.append("<xml>\r\n");

                if (StringHelper.hasAnyChar(actItem.appId)) {
                    arrayParam.add("mch_appid=" + actItem.appId);
                    sbXml.append("  <mch_appid>" + actItem.appId + "</mch_appid>\r\n");
                }

                if (StringHelper.hasAnyChar(actItem.merchantId)) {
                    arrayParam.add("mchid=" + actItem.merchantId);
                    sbXml.append("  <mchid>" + actItem.merchantId + "</mchid>\r\n");
                }

                String nonce_str = get_nonce_str();
                arrayParam.add("nonce_str=" + nonce_str);
                sbXml.append("  <nonce_str>" + nonce_str + "</nonce_str>\r\n");

                arrayParam.add("partner_trade_no=" + p_req1908.singleDetail.askNumber);
                sbXml.append("  <partner_trade_no>" + p_req1908.singleDetail.askNumber + "</partner_trade_no>\r\n");

                arrayParam.add("openid=" + p_req1908.singleDetail.recAct);
                sbXml.append("  <openid>" + p_req1908.singleDetail.recAct + "</openid>\r\n");

                String checkName = "NO_CHECK";
                if ("1".equalsIgnoreCase(p_req1908.singleDetail.realNameFlag) || getBankConfig().getValueAsBool("isCheckName")) {
                    checkName = "FORCE_CHECK";
                }
                arrayParam.add("check_name=" + checkName);
                sbXml.append("  <check_name>" + checkName + "</check_name>\r\n");

                arrayParam.add("re_user_name=" + p_req1908.singleDetail.recName);
                sbXml.append("  <re_user_name>" + p_req1908.singleDetail.recName + "</re_user_name>\r\n");

                arrayParam.add("amount=" + amount);
                sbXml.append("  <amount>" + amount + "</amount>\r\n");

                String body = p_req1908.singleDetail.purpose;
                if (StringHelper.isNullOrEmpty(body)) {
                    body = p_req1908.singleDetail.description;
                }
                if (StringHelper.isNullOrEmpty(body)) {
                    body = p_req1908.singleDetail.memo;
                }
                if (StringHelper.isNullOrEmpty(body)) {
                    body = p_req1908.singleDetail.useType;
                }
                body = XmlHelper.encode(body);
                arrayParam.add("desc=" + body);
                sbXml.append("  <desc>" + body + "</desc>\r\n");

                if (StringHelper.hasAnyChar(actItem.createIP)) {
                    arrayParam.add("spbill_create_ip=" + actItem.createIP);
                    sbXml.append("  <spbill_create_ip>" + actItem.createIP + "</spbill_create_ip>\r\n");
                }

                String[] signParams = new String[arrayParam.size()];
                arrayParam.toArray(signParams);
                String sign = signMessage(signParams, actItem.saltKey);
                sbXml.append("  <sign>" + sign + "</sign>\r\n");
                sbXml.append("</xml>");
            } else if ("190802A".equals(p_req1908.reqHeader.commandCode)) {
                PublicKey key = getPublicKey(actItem.ownCertFilePath);
                serviceUrlTrgFro = "paybank";
                sbXml.append("<xml>\r\n");
                arrayParam.add("mch_id=" + actItem.merchantId);
                sbXml.append("<mch_id>" + actItem.merchantId + "</mch_id>\r\n");
                String nonce_str = get_nonce_str();
                arrayParam.add("nonce_str=" + nonce_str);
                sbXml.append("<nonce_str>" + nonce_str + "</nonce_str>\r\n");
                arrayParam.add("partner_trade_no=" + p_req1908.singleDetail.askNumber);
                sbXml.append("<partner_trade_no>" + p_req1908.singleDetail.askNumber + "</partner_trade_no>\r\n");
                arrayParam.add("amount=" + amount);
                sbXml.append("<amount>" + amount + "</amount>\r\n");
                String bank_code = getBankConfig().getMapValueByKey("bankMapping", p_req1908.singleDetail.cnapsCode);
                arrayParam.add("bank_code=" + bank_code);
                sbXml.append("<bank_code>" + bank_code + "</bank_code>\r\n");
                String recAct = p_req1908.singleDetail.recAct;
                recAct = encryptData(key, recAct);
                arrayParam.add("enc_bank_no=" + recAct);
                sbXml.append("<enc_bank_no>" + recAct + "</enc_bank_no>\r\n");
                String recName = p_req1908.singleDetail.recName;
                recName = encryptData(key, recName);
                arrayParam.add("enc_true_name=" + recName);
                sbXml.append("<enc_true_name>" + recName + "</enc_true_name>\r\n");
                arrayParam.add("desc=" + p_req1908.singleDetail.memo);
                sbXml.append("<desc>" + p_req1908.singleDetail.memo + "</desc>\r\n");
                String[] signParam = new String[arrayParam.size()];
                arrayParam.toArray(signParam);
                String sign = signMessage(signParam, actItem.saltKey);
                sbXml.append("<sign>" + sign + "</sign>");
                sbXml.append("</xml>");
            } else if ("190803".equals(p_req1908.reqHeader.commandCode)) {
                PublicKey key = getPublicKeyFromWeiXin(actItem);//getPublicKey(actItem.ownCertFilePath);//
                serviceUrlTrgFro = "paybank";
                sbXml.append("<xml>\r\n");
                arrayParam.add("mch_id=" + actItem.merchantId);
                sbXml.append("  <mch_id>" + actItem.merchantId + "</mch_id>\r\n");

                arrayParam.add("partner_trade_no=" + p_req1908.singleDetail.askNumber);
                sbXml.append("  <partner_trade_no>" + p_req1908.singleDetail.askNumber + "</partner_trade_no>\r\n");

                String nonce_str = get_nonce_str();
                arrayParam.add("nonce_str=" + nonce_str);
                sbXml.append("  <nonce_str>" + nonce_str + "</nonce_str>\r\n");

                String enc_bank_no = p_req1908.singleDetail.recAct;
                enc_bank_no = encryptData(key, enc_bank_no);
                arrayParam.add("enc_bank_no=" + enc_bank_no);
                sbXml.append("  <enc_bank_no>" + enc_bank_no + "</enc_bank_no>\r\n");

                String enc_true_name = p_req1908.singleDetail.recName;
                enc_true_name = encryptData(key, enc_true_name);
                arrayParam.add("enc_true_name=" + enc_true_name);
                sbXml.append("  <enc_true_name>" + enc_true_name + "</enc_true_name>\r\n");

                String tmpBankCode = getBankConfig().getMapValueByKey("bankMapping", p_req1908.singleDetail.recBankCode);
                if (StringHelper.hasAnyChar(tmpBankCode)) {
                    arrayParam.add("bank_code=" + tmpBankCode);
                    sbXml.append("<bank_code>" + tmpBankCode + "</bank_code>\r\n");
                }

                arrayParam.add("amount=" + amount);
                sbXml.append("  <amount>" + amount + "</amount>\r\n");

                if (StringHelper.hasAnyChar(p_req1908.singleDetail.memo)) {
                    arrayParam.add("desc=" + p_req1908.singleDetail.memo);
                    sbXml.append("  <desc>" + p_req1908.singleDetail.memo + "</desc>\r\n");
                }

                String[] signParams = new String[arrayParam.size()];
                arrayParam.toArray(signParams);
                String sign = signMessage(signParams, actItem.saltKey);
                sbXml.append("  <sign>" + sign + "</sign>\r\n");
                sbXml.append("</xml>");
            } else if ("190807".equals(p_req1908.reqHeader.commandCode)) {
                serviceUrlTrgFro = "submitRecRefund";
                sbXml.append("<xml>\r\n");
                arrayParam.add("appid=" + actItem.appId);
                sbXml.append("  <appid>" + actItem.appId + "</appid>\r\n");
                arrayParam.add("mch_id=" + actItem.merchantId);
                sbXml.append("  <mch_id>" + actItem.merchantId + "</mch_id>\r\n");
                String nonce_str = get_nonce_str();
                arrayParam.add("nonce_str=" + nonce_str);
                sbXml.append("  <nonce_str>" + nonce_str + "</nonce_str>\r\n");
                String out_trade_no = p_req1908.singleDetail.memo;
                arrayParam.add("out_trade_no=" + out_trade_no);
                sbXml.append("  <out_trade_no>" + out_trade_no + "</out_trade_no>\r\n");
                String out_refund_no = p_req1908.singleDetail.recAct;
                arrayParam.add("out_refund_no=" + out_refund_no);
                sbXml.append("  <out_refund_no>" + out_refund_no + "</out_refund_no>\r\n");
                if (StringHelper.hasAnyChar(p_req1908.singleDetail.payAmount)) {
                    String total_fee = StringHelper.amountMultiply100(p_req1908.singleDetail.payAmount);
                    arrayParam.add("total_fee=" + total_fee);
                    sbXml.append("  <total_fee>" + total_fee + "</total_fee>\r\n");
                    String refund_fee = StringHelper.amountMultiply100(p_req1908.singleDetail.purpose);
                    arrayParam.add("refund_fee=" + refund_fee);
                    sbXml.append("  <refund_fee>" + refund_fee + "</refund_fee>\r\n");
                }
                String[] signParams = new String[arrayParam.size()];
                arrayParam.toArray(signParams);
                String sign = signMessage(signParams, actItem.saltKey);
                sbXml.append("  <sign>" + sign + "</sign>\r\n");
                sbXml.append("</xml>");
            } else if ("190808".equals(p_req1908.reqHeader.commandCode)) {
                //红包
                //PublicKey key = getPublicKey(actItem.ownCertFilePath);
                serviceUrlTrgFro = "sendRedpack";
                sbXml.append("<xml>\r\n");

                String nonce_str = get_nonce_str();
                arrayParam.add("nonce_str=" + nonce_str);
                sbXml.append("  <nonce_str><![CDATA[" + nonce_str + "]]></nonce_str>\r\n");

                //arrayParam.add("mch_appid=" + actItem.appId);
                //sbXml.append("  <mch_appid>" + actItem.appId + "</mch_appid>\r\n");

                arrayParam.add("mch_billno=" + p_req1908.singleDetail.askNumber);
                sbXml.append("  <mch_billno><![CDATA[" + p_req1908.singleDetail.askNumber + "]]></mch_billno>\r\n");

                arrayParam.add("mch_id=" + actItem.merchantId);
                sbXml.append("  <mch_id><![CDATA[" + actItem.merchantId + "]]></mch_id>\r\n");

                //String device_info = this.getBankConfig().getValueAsString("device_info");
                //arrayParam.add("device_info=" +device_info);
                //sbXml.append("  <device_info>"+device_info+"</device_info>\r\n");

                arrayParam.add("wxappid=" + actItem.appId);
                sbXml.append("  <wxappid><![CDATA[" + actItem.appId + "]]></wxappid>\r\n");

                arrayParam.add("send_name=" + p_req1908.singleDetail.payName);
                sbXml.append("  <send_name><![CDATA[" + p_req1908.singleDetail.payName + "]]></send_name>\r\n");

                arrayParam.add("re_openid=" + p_req1908.singleDetail.recAct);
                sbXml.append("  <re_openid><![CDATA[" + p_req1908.singleDetail.recAct + "]]></re_openid>\r\n");

                arrayParam.add("total_amount=" + amount);
                sbXml.append("  <total_amount><![CDATA[" + amount + "]]></total_amount>\r\n");

                arrayParam.add("total_num=" + "1");
                sbXml.append("  <total_num><![CDATA[1]]></total_num>\r\n");

                String wishing = StringHelper.getFirstNotEmptyTrim(p_req1908.singleDetail.description, p_req1908.singleDetail.memo, p_req1908.singleDetail.purpose, p_req1908.singleDetail.abstractATS);
                wishing = XmlHelper.encodeFull(wishing);
                arrayParam.add("wishing=" + wishing);
                sbXml.append("  <wishing><![CDATA[" + wishing + "]]></wishing>\r\n");

                arrayParam.add("client_ip=" + actItem.createIP);
                sbXml.append("  <client_ip><![CDATA[" + actItem.createIP + "]]></client_ip>\r\n");

                String actOrRemark = StringHelper.getFirstNotEmptyTrim(p_req1908.singleDetail.purpose, p_req1908.singleDetail.memo, p_req1908.singleDetail.description, p_req1908.singleDetail.abstractATS);
                actOrRemark = XmlHelper.encodeFull(actOrRemark);
                //活动名称 act_name
                arrayParam.add("act_name=" + actOrRemark);
                sbXml.append("  <act_name><![CDATA[" + actOrRemark + "]]></act_name>\r\n");

                //备注	remark
                arrayParam.add("remark=" + actOrRemark);
                sbXml.append("  <remark><![CDATA[" + actOrRemark + "]]></remark>\r\n");

                //场景id，发放红包使用场景，红包金额大于200或者小于1元时必传
                //amount的单位是分
                if (Integer.parseInt(amount) > 20000 || Integer.parseInt(amount) < 100) {
                    String scene_id = this.getBankConfig().getMapValueByContainsKey("sceneIdMapping", p_req1908.singleDetail.purpose);
                    arrayParam.add("scene_id=" + scene_id);
                    sbXml.append("  <scene_id><![CDATA[" + scene_id + "]]></scene_id>\r\n");

                } else {
                    //arrayParam.add("scene_id=" + "");
                    //sbXml.append("  <scene_id></scene_id>\r\n");
                }

                //arrayParam.add("risk_info=" + "");
                //sbXml.append("  <risk_info></risk_info>\r\n");

                String[] signParams = new String[arrayParam.size()];
                arrayParam.toArray(signParams);
                String sign = signMessage(signParams, actItem.saltKey);
                sbXml.append("  <sign><![CDATA[" + sign + "]]></sign>\r\n");
                sbXml.append("</xml>");
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = "指令编码不匹配";
                retAts.writeRetDetail(retDetail);
                return retAts;
            }

            String[] tmpStrRet = sendToBank(sbXml.toString(), serviceUrlTrgFro, actItem);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder sbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], sbMsg);
                if (tmpBkReader != null) {
                    String return_code = tmpBkReader.getFirstText("/xml/return_code");
                    String result_code = tmpBkReader.getFirstText("/xml/result_code");
                    if (return_code != null && return_code.equals("SUCCESS")) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.getFirstText("/xml/return_msg");
                        if (StringHelper.hasAnyChar(tmpBkReader.getFirstText("/xml/err_code_des"))) {
                            retDetail.respInfo = tmpBkReader.getFirstText("/xml/err_code_des");
                        }

                        retDetail.transState = return_code + "+" + result_code;
                        retDetail.payInfoCode = result_code;
                        retDetail.outTransID = tmpBkReader.getFirstText("/xml/payment_no");
                        if (StringHelper.hasAnyChar(tmpBkReader.getFirstText("/xml/mch_billno"))) {
                            retDetail.outTransID = tmpBkReader.getFirstText("/xml/mch_billno");
                        }
                        if (StringHelper.hasAnyChar(tmpBkReader.getFirstText("/xml/err_code"))) {
                            retDetail.transState = retDetail.transState + "+" + tmpBkReader.getFirstText("/xml/err_code");
                            retDetail.payInfoCode = tmpBkReader.getFirstText("/xml/err_code");
                            retDetail.respInfo = tmpBkReader.getFirstText("/xml/err_code_des");
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpBkReader.getFirstText("/xml/return_msg");
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = sbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            retDetail.respCode = "-1";
            retDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1944);

        try {
            ActSetItem actSetItem = getBankConfig().getActSetItem(p_req1944.payAct, "wxpay");
            if (actSetItem == null) {
                retDetail.respCode = "-1";
                retDetail.respInfo = "未找到账号[" + p_req1944.payAct + "]商户配置信息";
                retAts.writeRetDetail(retDetail);
                return retAts;
            }

            ArrayList<String> arrayParams = new ArrayList<String>();
            StringBuilder sbXml = new StringBuilder();
            String serviceUrlTrgFor = "";
            if ("190802".equals(p_req1944.reqHeader.commandCode) || "WXTransfer01".equals(p_req1944.reqHeader.commandCode)) {
                serviceUrlTrgFor = "gettransferinfo";
                sbXml.append("<xml>\r\n");
                arrayParams.add("partner_trade_no=" + p_req1944.askNumber);
                sbXml.append("  <partner_trade_no><![CDATA[" + p_req1944.askNumber + "]]></partner_trade_no>\n");
                arrayParams.add("mch_id=" + actSetItem.merchantId);
                sbXml.append("  <mch_id><![CDATA[" + actSetItem.merchantId + "]]></mch_id>\n");
                arrayParams.add("appid=" + actSetItem.appId);
                sbXml.append("  <appid><![CDATA[" + actSetItem.appId + "]]></appid>\n");
                String nonce_str = get_nonce_str();
                arrayParams.add("nonce_str=" + nonce_str);
                sbXml.append("  <nonce_str><![CDATA[" + nonce_str + "]]></nonce_str>\n");
                String[] signParams = new String[arrayParams.size()];
                arrayParams.toArray(signParams);
                String sign = signMessage(signParams, actSetItem.saltKey);
                sbXml.append("  <sign><![CDATA[" + sign + "]]></sign>\n");
                sbXml.append("</xml>");
            } else if ("190802A".equals(p_req1944.reqHeader.commandCode)) {
                serviceUrlTrgFor = "querybank";
                sbXml.append("<xml>");
                arrayParams.add("mch_id=" + actSetItem.merchantId);
                sbXml.append("<mch_id>" + actSetItem.merchantId + "</mch_id>");
                String nonce_str = get_nonce_str();
                arrayParams.add("nonce_str=" + nonce_str);
                sbXml.append("<nonce_str>" + nonce_str + "</nonce_str>");
                arrayParams.add("partner_trade_no=" + p_req1944.askNumber);
                sbXml.append("<partner_trade_no>" + p_req1944.askNumber + "</partner_trade_no>");
                String[] signParams = new String[arrayParams.size()];
                arrayParams.toArray(signParams);
                String sign = signMessage(signParams, actSetItem.saltKey);
                sbXml.append("<sign>" + sign + "</sign>");
                sbXml.append("</xml>");
            } else if ("190803".equals(p_req1944.reqHeader.commandCode)) {
                serviceUrlTrgFor = "querybank";
                sbXml.append("<xml>\r\n");
                arrayParams.add("mch_id=" + actSetItem.merchantId);
                sbXml.append("  <mch_id><![CDATA[" + actSetItem.merchantId + "]]></mch_id>\n");

                arrayParams.add("partner_trade_no=" + p_req1944.askNumber);
                sbXml.append("  <partner_trade_no><![CDATA[" + p_req1944.askNumber + "]]></partner_trade_no>\n");

                String nonce_str = get_nonce_str();
                arrayParams.add("nonce_str=" + nonce_str);
                sbXml.append("  <nonce_str><![CDATA[" + nonce_str + "]]></nonce_str>\n");

                arrayParams.add("sign_type=" + "MD5");
                sbXml.append("  <sign_type><![CDATA[" + "MD5" + "]]></sign_type>\n");

                String[] signParams = new String[arrayParams.size()];
                arrayParams.toArray(signParams);
                String sign = signMessage(signParams, actSetItem.saltKey);
                sbXml.append("  <sign><![CDATA[" + sign + "]]></sign>\n");
                sbXml.append("</xml>");
                System.out.println("sign:\r\n" + sign);
            } else if ("190807".equals(p_req1944.reqHeader.commandCode)) {
                serviceUrlTrgFor = "queryRecRefund";
                sbXml.append("<xml>\r\n");
                arrayParams.add("appid=" + actSetItem.appId);
                sbXml.append("  <appid>" + actSetItem.appId + "</appid>\r\n");
                arrayParams.add("mch_id=" + actSetItem.merchantId);
                sbXml.append("  <mch_id>" + actSetItem.merchantId + "</mch_id>\r\n");
                String nonce_str = get_nonce_str();
                arrayParams.add("nonce_str=" + nonce_str);
                sbXml.append("  <nonce_str>" + nonce_str + "</nonce_str>\r\n");
                String out_trade_no = p_req1944.recAct;
                arrayParams.add("out_trade_no=" + out_trade_no);
                sbXml.append("  <out_trade_no>" + out_trade_no + "</out_trade_no>\r\n");
                String[] signParams = new String[arrayParams.size()];
                arrayParams.toArray(signParams);
                String sign = signMessage(signParams, actSetItem.saltKey);
                sbXml.append("  <sign><![CDATA[" + sign + "]]></sign>\n");
                sbXml.append("</xml>");
            } else if ("190808".equals(p_req1944.reqHeader.commandCode)) {
                serviceUrlTrgFor = "queryRedpack";
                sbXml.append("<xml>\r\n");
                String nonce_str = get_nonce_str();
                arrayParams.add("nonce_str=" + nonce_str);
                sbXml.append("  <nonce_str>" + nonce_str + "</nonce_str>\r\n");
                arrayParams.add("mch_billno=" + p_req1944.askNumber);
                sbXml.append("  <mch_billno>" + p_req1944.askNumber + "</mch_billno>\r\n");
                arrayParams.add("mch_id=" + actSetItem.merchantId);
                sbXml.append("  <mch_id>" + actSetItem.merchantId + "</mch_id>\r\n");
                arrayParams.add("appid=" + actSetItem.appId);
                sbXml.append("  <appid>" + actSetItem.appId + "</appid>\r\n");
                arrayParams.add("bill_type=" + "MCHT");
                sbXml.append("  <bill_type>" + "MCHT" + "</bill_type>\r\n");
                String[] signParams = new String[arrayParams.size()];
                arrayParams.toArray(signParams);
                String sign = signMessage(signParams, actSetItem.saltKey);
                sbXml.append("  <sign><![CDATA[" + sign + "]]></sign>\n");
                sbXml.append("</xml>");
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = "指令编码不匹配";
                retAts.writeRetDetail(retDetail);
                return retAts;
            }

            String[] tmpStrRet = sendToBank(sbXml.toString(), serviceUrlTrgFor, actSetItem);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder sbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], sbMsg);
                if (tmpBkReader != null) {
                    String return_code = tmpBkReader.getFirstText("/xml/return_code");
                    String result_code = tmpBkReader.getFirstText("/xml/result_code");
                    if (return_code != null && return_code.equals("SUCCESS")) {
                        if ("190807".equals(p_req1944.reqHeader.commandCode)) {
                            retDetail.respCode = "0";
                            retDetail.respInfo = tmpBkReader.getFirstText("/xml/return_msg");
                            retDetail.transState = return_code + "+" + result_code;
                            retDetail.payInfoCode = tmpBkReader.getFirstText("/xml/refund_status_0");
                            retDetail.transAmt = tmpBkReader.getFirstText("/xml/refund_fee");
                            retDetail.outTransID = tmpBkReader.getFirstText("/xml/out_trade_no");
                        } else if ("190808".equals(p_req1944.reqHeader.commandCode)) {
                            retDetail.respCode = "0";
                            retDetail.respInfo = tmpBkReader.getFirstText("/xml/return_msg");
                            retDetail.transState = return_code + "+" + result_code;
                            retDetail.payInfoCode = tmpBkReader.getFirstText("/xml/status");
                            retDetail.transAmt = tmpBkReader.getFirstText("/xml/total_amount");
                            retDetail.outTransID = tmpBkReader.getFirstText("/xml/mch_billno");
                        } else {
                            retDetail.respCode = "0";
                            retDetail.respInfo = tmpBkReader.getFirstText("/xml/return_msg");
                            if (StringHelper.hasAnyChar(tmpBkReader.getFirstText("/xml/err_code_des"))) {
                                retDetail.respInfo = tmpBkReader.getFirstText("/xml/err_code_des");
                            }
                            if (StringHelper.hasAnyChar(tmpBkReader.getFirstText("/xml/reason"))) {
                                retDetail.respInfo = tmpBkReader.getFirstText("/xml/reason");
                            }
                            retDetail.outTransID = tmpBkReader.getFirstText("/xml/detail_id");
                            retDetail.transAmt = StringHelper.amtDiv100RetZeroIfNullOrEmpty(tmpBkReader.getFirstText("/xml/payment_amount"));

                            retDetail.transState = return_code + "+" + result_code;
                            if (StringHelper.hasAnyChar(tmpBkReader.getFirstText("/xml/err_code"))) {
                                retDetail.transState = retDetail.transState + "+" + tmpBkReader.getFirstText("/xml/err_code");
                                retDetail.payInfoCode = tmpBkReader.getFirstText("/xml/err_code");
                                retDetail.respInfo = tmpBkReader.getFirstText("/xml/err_code_des");
                            }
                            if (StringHelper.hasAnyChar(tmpBkReader.getFirstText("/xml/status"))) {
                                retDetail.transState = retDetail.transState + "+" + tmpBkReader.getFirstText("/xml/status");
                                retDetail.payInfoCode = tmpBkReader.getFirstText("/xml/status");
                                if (StringHelper.hasAnyChar(tmpBkReader.getFirstText("/xml/reason"))) {
                                    retDetail.payInfoCode = tmpBkReader.getFirstText("/xml/reason");
                                    retDetail.respInfo = tmpBkReader.getFirstText("/xml/reason");
                                }
                            }
                        }
                        String errCode = tmpBkReader.getFirstText("/xml/err_code");
                        if ("ORDERNOTEXIST".equals(errCode)) {
                            retDetail.respCode = "0";
                            retDetail.respInfo = tmpBkReader.getFirstText("/xml/err_code_des");
                            retDetail.transState = tmpBkReader.getFirstText("/xml/err_code");
                            retDetail.payInfoCode = tmpBkReader.getFirstText("/xml/err_code");
                        }
                        if (StringHelper.hasAnyChar(retDetail.transState) && StringHelper.hasAnyChar(retDetail.payInfoCode)) {
                            if (retDetail.transState.contains("SYSTEMERROR") || retDetail.payInfoCode.contains("SYSTEMERROR")) {
                                retDetail.respCode = "-1";
                            }
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpBkReader.getFirstText("/xml/return_msg");
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = sbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retDetail.respCode = "-1";
            retDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}